﻿using Connection.IView;
using Connection.Presenter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace RemoChat
{
    /// <summary>
    /// Interaction logic for WinOnline.xaml
    /// </summary>
    public partial class WinOnline : Window, IWinInviChatView
    {
        string URL;
        string _toUsername;

        InviPresenter invipresenter;

        public WinOnline()
        {
            InitializeComponent();
            for (int i = 0; i < 5; i++)
            {
                Button newBtn = new Button();
                newBtn.Content = i.ToString();
                newBtn.Name = "Button" + i.ToString();
                splOnline.Children.Add(newBtn);
            }
        }

        public WinOnline(string url, string fromusername)
        {
            InitializeComponent();
            URL = url;
            this.FromUsername = fromusername;

            DispatcherTimer dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
            dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
            dispatcherTimer.Start();
        }

        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            Init();
        }

        void Init()
        {
            invipresenter = (InviPresenter)Activator.GetObject(typeof(InviPresenter), URL + "PRESENTER/INVIPRESENTER_OBJECT");
            List<String> temp = invipresenter.ListOnline();
            if (temp != null)
            {
                splOnline.Children.Clear();
                for (int i = 0; i < temp.Count; i++)
                {
                    Button btnOnline = new Button();
                    btnOnline.Content = temp[i];//i.ToString();
                    btnOnline.FontSize = 20;
                    btnOnline.Background = new SolidColorBrush(Colors.White);
                    btnOnline.Name = temp[i];
                    btnOnline.Height = 50;
                    btnOnline.Click += this.btnOnline_Click;
                    splOnline.Children.Add(btnOnline);
                }
            }
        }

        private void btnOnline_Click(object sender, EventArgs e)
        {
            var btnClicked = sender as Button;
            InviPresenter inviPresenter = (InviPresenter)Activator.GetObject(typeof(InviPresenter), URL + "PRESENTER/INVIPRESENTER_OBJECT");
            this.ToUsername = btnClicked.Name.ToString();
            if (inviPresenter.IsInviOk(this.ToUsername))
            {
                WinChat winchat = new WinChat(URL, this.ToUsername, this.FromUsername);
                winchat.Show();
            }
            else
                MessageBox.Show("That Bai");
        }

        public string ToUsername
        {
            get { return _toUsername; }
            set { _toUsername = value; }
        }

        public string FromUsername
        {
            get { return lbFromUsername.Content.ToString(); }
            set { lbFromUsername.Content = value; }
        }

        private void mniLognOut_Click(object sender, RoutedEventArgs e)
        {
            LoginPresenter loginPresenter = (LoginPresenter)Activator.GetObject(typeof(LoginPresenter), URL + "PRESENTER/LOGINPRESENTER_OBJECT");
            loginPresenter.UpdateIsOnline(this.FromUsername, 0);
            for (int intCounter = App.Current.Windows.Count - 1; intCounter > 0; intCounter--)
                App.Current.Windows[intCounter].Close();
            App.Current.Windows[0].Show();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            LoginPresenter loginPresenter = (LoginPresenter)Activator.GetObject(typeof(LoginPresenter), URL + "PRESENTER/LOGINPRESENTER_OBJECT");
            loginPresenter.UpdateIsOnline(this.FromUsername, 0);
            for (int intCounter = App.Current.Windows.Count - 1; intCounter > 0; intCounter--)
                App.Current.Windows[intCounter].Close();
            App.Current.Windows[0].Show();
        }
    }
}
